/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;


namespace Shuriken.Core.Store.Items
{      
    /// <summary>
    /// Search url item
    /// </summary>
    public class SearchUrlItem : UrlItem 
    {
        public static string PLACEHOLDER = "***";
        /// <summary>
        /// Default constructor
        /// </summary>
        public SearchUrlItem()
            : base()
        {
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="folderItem"></param>
        /// <param name="store"></param>
        public SearchUrlItem(Shell32.FolderItem folderItem)
            : base(folderItem)
        {
        }

        /// <summary>
        /// Registering Actions 
        /// </summary>
        /// <remarks>Replacing the default action ("Execute") with a new default action ("Search for").</remarks>
        public override void RegisterActions()
        {
            base.RegisterActions();

            ActionManager am = ActionManager.GetDefaultInstance(); 

            // Adding "Search for" action
            am.RegisterAction(this.GetType(), new SearchUrlSearchForAction());

            // Removing the "Execute" default action 
            am.RemoveAction(this.GetType(), (new Shell32FolderItemOpenAction()).Name);
        }

        public string ReplaceAndFormatQuery(string url, string query)
        {
            string escapedurl = System.Uri.EscapeDataString(query);
            return url.Replace(SearchUrlItem.PLACEHOLDER, escapedurl);
        }

        /// <summary>
        /// Replaces the placeholder (***) with the Attribute and executes the Url
        /// </summary>
        /// <param name="attribute">Attribute</param>
        internal void ReplaceAndExecuteUrl(string attribute)
        {
            string replacedUrl = ReplaceAndFormatQuery(Url, attribute);
            ExecuteUrl(replacedUrl);
        }
    }

    [DefaultAction]
    [ActionName("Search for")]
    [ActionSubjectType(typeof(SearchUrlItem))]
    public class SearchUrlSearchForAction : Shell32FolderItemBaseAction
    {
        public SearchUrlSearchForAction()
            : base()
        {
            m_IsAttributed = true; 
        }
        /// <summary>
        /// Executes this action 
        /// </summary>
        /// <param name="subject">subject to execute on</param>
        public override void Execute(Shell32FolderItem subject)
        {
            // there will be some magic here to replace the placeholder [***]
            // with the search term

            if (subject != null && subject is SearchUrlItem)
            {
                SearchUrlItem searchURL = (SearchUrlItem)subject;
                searchURL.ReplaceAndExecuteUrl(ActionAttribute);
            }
        }
    }
}
